﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Drawing;
using System.Windows;
using System.Windows.Forms;

namespace carParking
{
    class SerialCommunication
    {
        //difine
        //public enum transmisstionType {Text,Hex}
        //public enum messageType {inCome,outCome,normal,warning,error}

        //variable
        private string _baudRate = string.Empty;
        private string _parity = string.Empty;
        private string _stopBits = string.Empty;
        private string _dataBits = string.Empty;
        private string _portName = string.Empty;
        //private transmisstionType _transType;

        //Global variable
        private SerialPort comPort = new SerialPort();

        //constructors
        public SerialCommunication(string baud,string par,string sBits,string dBits,string pName){
            this._baudRate = baud;
            this._parity = par;
            this._stopBits = sBits;
            this._dataBits = dBits;
            this._portName = pName;
        }

        public SerialCommunication(){
            this._baudRate = string.Empty;
            this._parity = string.Empty;
            this._stopBits = string.Empty;
            this._dataBits = string.Empty;
            this._portName = "COM1";
        }

        public string portName{
            get { return _portName; }
            set { _portName = value; }
        }

        public Boolean openPort(){
            try{
                //close port before open again
                if (comPort.IsOpen == true){
                    comPort.Close();
                }
                //set paramiter
                comPort.BaudRate = int.Parse(_baudRate);
                comPort.DataBits = int.Parse(_dataBits);
                comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits),_stopBits);
                comPort.Parity = (Parity)Enum.Parse(typeof(Parity),_parity);
                comPort.PortName = _portName;

                //open port
                comPort.Open();
                return true;
            }
            catch(Exception ex){
                //Console.WriteLine(ex.Message);
                System.Windows.Forms.MessageBox.Show("Error :" + ex.Message, "Warning");
                return false;
            }
        }

        public Boolean closePort(){
            if (comPort.IsOpen)
            {
                comPort.Close();
                return true;
            }
            return false;
        }

        public SerialPort getSerialPort(){
            return comPort;
        }

        public void sendData(string msg){
            try
            {
                comPort.WriteLine(msg + "\r\n");
            }
            catch(Exception ex){
                MessageBox.Show("Error : " + ex.Message);
            }
        }

        public void sendByteToSerial(byte [] n1,int length){
            try {
                comPort.Write(n1,0,length);
            }
            catch(Exception ex){
                MessageBox.Show("Error : "+ex.Message);
            }
        }
    }
}
